package com.ruoyi.web.controller.system;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.params.CustomerSaveParams;
import com.ruoyi.system.domain.vo.UmsCustomerListVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.UmsCustomer;
import com.ruoyi.system.service.IUmsCustomerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客户Controller
 *
 * @author ruoyi
 * @date 2023-02-26
 */
@RestController
@RequestMapping("/system/customer")
public class UmsCustomerController extends BaseController {
    @Resource
    private IUmsCustomerService umsCustomerService;

    /**
     * 查询客户列表
     */
    @PreAuthorize("@ss.hasPermi('system:customer:list')")
    @GetMapping("/list")
    public TableDataInfo list(UmsCustomer umsCustomer) {
        startPage();
        List<UmsCustomerListVo> list = umsCustomerService.selectUmsCustomerList(umsCustomer);
        return getDataTable(list);
    }

    /**
     * 导出客户列表
     */
    @PreAuthorize("@ss.hasPermi('system:customer:export')")
    @Log(title = "客户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UmsCustomer umsCustomer) {
        List<UmsCustomerListVo> list = umsCustomerService.selectUmsCustomerList(umsCustomer);
        ExcelUtil<UmsCustomerListVo> util = new ExcelUtil<UmsCustomerListVo>(UmsCustomerListVo.class);
        util.exportExcel(response, list, "客户数据");
    }

    /**
     * 获取客户详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:customer:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Integer userId) {
        return success(umsCustomerService.selectUmsCustomerByUserId(userId));
    }

    /**
     * 新增客户
     */
    @PreAuthorize("@ss.hasPermi('system:customer:add')")
    @Log(title = "客户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UmsCustomer umsCustomer) {
        umsCustomer.setCreateBy(getUserId().toString());
        umsCustomer.setCreateTime(DateUtils.getNowDate());
        return toAjax(umsCustomerService.insertUmsCustomer(umsCustomer));
    }

    /**
     * 新增客户
     */
    @PreAuthorize("@ss.hasPermi('system:customer:add')")
    @Log(title = "生成客户代码", businessType = BusinessType.INSERT)
    @PostMapping("/create/code")
    public AjaxResult createUserCode(@RequestParam String username) {
        return AjaxResult.success("获取成功",umsCustomerService.createUserCode(username));
    }

    /**
     * 修改客户
     */
    @PreAuthorize("@ss.hasPermi('system:customer:edit')")
    @Log(title = "客户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerSaveParams umsCustomer) {
        umsCustomer.setUpdateBy(getUserId().toString());
        umsCustomer.setUpdateTime(DateUtils.getNowDate());
        return toAjax(umsCustomerService.updateUmsCustomer(umsCustomer));
    }

    /**
     * 删除客户
     */
    @PreAuthorize("@ss.hasPermi('system:customer:remove')")
    @Log(title = "客户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds) {
        return toAjax(umsCustomerService.deleteUmsCustomerByUserIds(userIds));
    }
}
